import axios from 'axios';
import {getUserInfo} from "./util";
import {message,Spin} from "ant-design-vue";

axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
let request = axios.create({
    baseURL: apiPath,
    timeout: 30000,
    withCredentials: false
});

request.interceptors.request.use(config => {
    //在请求头加入token，名字要和后端接收请求头的token名字一样
    const userInfo = getUserInfo();
    if(null != userInfo){
        config.headers.token = getUserInfo().token;
    }

    return config;
}, error => {
    // 对请求错误做些什么
    return Promise.reject(error);
});
// 添加响应拦截器
request.interceptors.response.use(response => {
    if(response.data.code==402){
        console.log(response.data)
        message.warn("token过期，请重新登录！")
    }
    if(response.data.code==500){
        console.log(response.data)
        message.error(response.data)
    }
    return response;
}, error => {
    return Promise.reject(error);
});


export default request;